'use strict';

const Controller = require('egg').Controller;

class SystemController extends Controller {
  /*
     * 系统设置
     **/
  async index() {
    const group_list = { shop_info: '网站信息', sms: '短信设置' };
    const ctx = this.ctx;
    const method = ctx.request.method;
    if (method === 'POST') {
      const data = ctx.request.body;
      const inc_type = ctx.request.body.inc_type;
      let body = { status: 0, message: '参数错误' };
      if (group_list[inc_type]) {
        let res = await ctx.service.config.systemConfig(inc_type, data);
        if (res) {
          await ctx.service.redis.getSiteConfig(true);
          body = { status: 1, message: '操作成功' };
        }
      }
      ctx.body = body;
      ctx.status = 200;
    } else {
      const inc_type = ctx.request.query.inc_type || 'shop_info';
      const config = await ctx.service.config.systemConfig(inc_type);
      const data_list = {
        group_list,
        inc_type,
        config,
      };
      await ctx.render(ctx.view_path(), data_list);
    }
  }

  /*
     * 菜单列表
     **/
  async menu() {
    const ctx = this.ctx;
    const method = ctx.request.method;

    if (method === 'POST') {
      const mod_id = ctx.request.body.id || 0;
      const orderby = ctx.request.body.orderby || 50;
      const body = { message: '排序失败' };
      if (mod_id && parseInt(orderby)) {
        const res = await ctx.model.Systemmodule.update({ orderby }, { where: { mod_id } });
        if (res[0]) body.message = '排序成功';
      }
      ctx.body = body;
      ctx.status = 200;
    } else {
      const tree = await ctx.service.admin.getRoleMenuList();
      const data_list = {
        tree,
      };
      await ctx.render(ctx.view_path(), data_list);
    }

  }

  /*
     * 创建菜单
     **/
  async create_menu() {
    const ctx = this.ctx;
    const method = ctx.request.method;
    if (method === 'POST') {
      const act = ctx.request.body.act;
      const title = ctx.request.body.title;
      const parent_id = ctx.request.body.parent_id;
      const orderby = ctx.request.body.orderby;
      const icon = ctx.request.body.icon;
      const body = { status: 0 };
      if (title) {
        const data = { title, parent_id, orderby, icon };
        switch (act) {
          case 'add': {
            if (parent_id > 0) {
              data.level = 2;
              data.module = 'menu';
            } else {
              data.level = 1;
              data.module = 'top';
            }
            const res = await ctx.model.Systemmodule.create(data, { raw: true });
            if (res) body.status = 1;
            break;
          }
          case 'edit': {
            const mod_id = ctx.request.body.mod_id;
            if (mod_id) {
              const res = await ctx.model.Systemmodule.update(data, { where: { mod_id } });
              if (res[0]) body.status = 1;
            }
            break;
          }
          default:
            break;
        }
      }
      ctx.body = body;
      ctx.status = 200;
    } else {
      const tree = await ctx.service.admin.getRoleMenuList();
      const act = ctx.request.query.act || '';
      const mod_id = ctx.request.query.mod_id || 0;
      let menu = {};
      if (mod_id) {
        menu = await ctx.model.Systemmodule.find({ where: { mod_id } });
      }
      const data_list = {
        tree,
        pid: mod_id,
        act,
        menu,
      };
      await ctx.render(ctx.view_path(), data_list);
    }
  }

  /*
     * 模块控制
     **/
  async ctl_detail() {
    const ctx = this.ctx;
    const method = ctx.request.method;
    if (method === 'POST') {
      const body = { status: 0 };
      const module_list = ctx.request.body.module || [];
      const add_data = ctx.request.body.data || [];
      const module_keys = Object.keys(module_list);
      if (module_keys.length) { // 更新的列
        for (const k in module_list) {
          const item = module_list[k];
          item.visible = parseInt(item.visible) === 1 ? 1 : 0;
          const set_res = await ctx.model.Systemmodule.update(item, { where: { mod_id: k } });
          if (set_res[0]) body.status = 1;
        }
      }
      if (add_data.length) { // 新增的列
        const all_data = [];
        for (const item of add_data) {
          if (item.title && item.ctl && item.act) {
            item.level = 3;
            item.module = 'module';
            item.orderby = item.orderby ? item.orderby : 50;
            all_data.push(item);
          }
        }
        if (all_data.length) {
          const add_res = await ctx.model.Systemmodule.bulkCreate(all_data, { raw: true });
          if (add_res) body.status = 1;
        }
      }
      ctx.body = body;
      ctx.status = 200;
    } else {
      const mod_id = ctx.request.query.mod_id || 0;
      const modules = await ctx.model.Systemmodule.findAll({
        where: { parent_id: mod_id, module: 'module' },
        order: [[ 'mod_id', 'asc' ]],
        rwa: true,
      });
      const tree = await ctx.service.admin.getRoleMenuList();
      const data_list = {
        pid: mod_id,
        modules,
        menu_tree: tree,
      };
      await ctx.render(ctx.view_path(), data_list);
    }
  }

  /*
     * 模块列表
     **/
  async module() {
    const tree = await this.ctx.service.admin.getRoleMenuList();
    const data_list = {
      tree,
    };
    await this.ctx.render(this.ctx.view_path(), data_list);
  }

}

module.exports = SystemController;
